using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Org.Eurekaa.DNJ.Ext.Common;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Drawing.Design;
using System.Drawing;
using Org.Eurekaa.DNJ.Ext.PluginsData.Common;
using Org.Eurekaa.DNJ.Ext.PluginsData.Cycle;
using Org.Eurekaa.DNJ.Helpers;
using Org.Eurekaa.DNJ.UI.WebControls;
using Org.Eurekaa.DNJ.Settings;
using Org.Eurekaa.DNJ.Ext.DNJSettings;
using Org.Eurekaa.DNJ.Collections;

namespace Org.Eurekaa.DNJ.Ext.UI.WebControls
{
    [Serializable()]
    public class ImageFile
    {
        [Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string Url { get; set; }

    }

    [ToolboxData("<{0}:Cycle Height=\"200px\" Width=\"200px\" runat=server></{0}:Cycle>")]
    [ToolboxBitmap(typeof(System.Web.UI.WebControls.ImageMap))]
    [Designer(typeof(Org.Eurekaa.DNJ.UI.Design.WebControls.JQueryUIControlDesigner))]
    [ParseChildren(true)]
    [PersistChildren(false)]
    [DefaultProperty("Settings")]
    public class Cycle : JQueryUIControl, IDNJPlugin
    {
        private HtmlGenericControl CycleDiv = new HtmlGenericControl("div");


        private DNJGenericCollection<ImageFile> images = new DNJGenericCollection<ImageFile>();
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]  // Content generates code for each page
        //[Editor(typeof(ImageCollectionEditor), typeof(UITypeEditor))]
        [DefaultValue(""), Category("$.DNJ Settings")]        
        public DNJGenericCollection<ImageFile> Images
        {
            get { return images; }
        }
        

        private CycleSettings _settings = new CycleSettings();        
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("Settings")]
        [PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public CycleSettings Settings
        {
            get { return _settings; }
            set { _settings = value; }
        }


        public override void SaveSettings(ref object stateObject)
        {
            object[] lstObj = new object[2];
            lstObj[0] = Settings;
            //lstObj[1] = ClientEvents;

            stateObject = lstObj;
            GenericSettingsObject allSettings = new GenericSettingsObject();


            //PageHelper.AppendJQueryScript(ClientEvents.GetDefaultEventsScript(), this.Page.Header);

            allSettings.Append(Settings);
            //allSettings.Append(ClientEvents);

            
            //PageHelper.AppendJQueryPlugin("#" + this.ClientID, "tabs", allSettings.ToString(), this.Page.Header);
            PageHelper.AppendJQueryPlugin("#" + CycleDiv.ClientID, "cycle", allSettings.ToString(), Page.Header);
            PageHelper.RegisterDNJResource("easing", this.Page.Header);
            PageHelper.RegisterDNJResource("cycle", this.Page.Header);

        }
        public override void LoadSettings(object stateObject)
        {
            //Settings = (stateObject as DNJTabsSettings);
            object[] lstObj = (object[])stateObject;
            Settings = (lstObj[0] as CycleSettings);
            //ClientEvents = (lstObj[1] as DNJTabsEvents);
        }


        protected override void CreateChildControls()
        {
            //base.CreateChildControls();
            
            CycleDiv.ID = this.ID + "_container";
            
            if (DesignMode)
            {
                CycleDiv.Attributes.Add("style", "overflow:hidden;border:1px solid #aaa;background:#eee;width:100%;height:100%");
                CycleDiv.InnerHtml = "&nbsp;";
                
            }
            if (images != null)
            {
                if (DesignMode)
                {
                    System.Web.UI.WebControls.Image i = new System.Web.UI.WebControls.Image();
                    i.ImageUrl = Images[0].Url;
                    i.Width = this.Width;
                    i.Height = this.Height;
                    CycleDiv.Controls.Add(i);
                }
                else
                {
                    foreach (ImageFile img in Images)
                    {
                        System.Web.UI.WebControls.Image i = new System.Web.UI.WebControls.Image();
                        i.ImageUrl = img.Url;
                        i.Width = this.Width;
                        i.Height = this.Height;
                        CycleDiv.Controls.Add(i);
                    }
                }
            }

            if (!DesignMode)
            {

                Org.Eurekaa.DNJ.Config.ResourcesConfig.AddResource("cycle", "Org.Eurekaa.DNJ.Ext.PluginsData.Cycle.jquery.cycle.all.min.js");
                Org.Eurekaa.DNJ.Config.ResourcesConfig.AddResource("easing", "Org.Eurekaa.DNJ.Ext.PluginsData.Common.jquery.easing.js");
                //PageHelper.RegisterHeaderScript(Page.ClientScript.GetWebResourceUrl(typeof(CommonResources), "DNJ.Ext.PluginsData.Common.jquery.easing.js"), Page.Header);
                //PageHelper.RegisterHeaderScript(Page.ClientScript.GetWebResourceUrl(typeof(CycleResources), "DNJ.Ext.PluginsData.Cycle.jquery.cycle.all.min.js"), Page.Header);
                
            }
            Controls.Add(CycleDiv);
        }

        #region IDNJPlugin Members
        public string Version()
        {
            return "2.65 (07-APR-2009)";
        }

        public bool Embedded()
        {
            return true;
        }
        #endregion


    }


    
}
